package com.group4.xml;

import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.group4.DAO.VideoDAO;
import com.group4.DB.Video;

/**
 * @author yaseminar
 *
 */
public class XmlResponse {

    public static String returnVideoXML(VideoDAO video) {

        String resultString = null;
        try {

            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            // root elements
            Document doc = docBuilder.newDocument();
            Element rootElement = doc.createElement("operation");
            doc.appendChild(rootElement);

            Element result = doc.createElement("result");
            result.appendChild(doc.createTextNode(video != null ? "1" : "0"));
            rootElement.appendChild(result);

            if (video != null) {

                Element videoElement = doc.createElement("video");
                rootElement.appendChild(videoElement);

                Element id = doc.createElement("ID");
                id.appendChild(doc.createTextNode(video.getID() > 0 ? String.valueOf(video.getID())
                        : ""));
                videoElement.appendChild(id);

                Element video_name = doc.createElement("video_name");
                video_name.appendChild(doc.createTextNode(video.getVideoName() != null
                        ? video.getVideoName() : ""));
                videoElement.appendChild(video_name);

                Element video_url = doc.createElement("video_url");
                video_url.appendChild(doc.createTextNode(video.getVideoUrl() != null
                        ? video.getVideoUrl() : ""));
                videoElement.appendChild(video_url);

                Element video_uploadDateTime = doc.createElement("video_uploadDateTime");
                video_uploadDateTime.appendChild(doc.createTextNode(video.getVideoUploadDateTime() != null
                        ? String.valueOf(video.getVideoUploadDateTime()) : ""));
                videoElement.appendChild(video_uploadDateTime);

                Element video_uploadLocation = doc.createElement("video_uploadLocation");
                video_uploadLocation.appendChild(doc.createTextNode(video.getVideoUploadLocation() != null
                        ? video.getVideoUploadLocation() : ""));
                videoElement.appendChild(video_uploadLocation);

                Element video_description = doc.createElement("video_description");
                video_description.appendChild(doc.createTextNode(video.getVideoDescription() != null
                        ? video.getVideoDescription() : ""));
                videoElement.appendChild(video_description);

                Element video_rate = doc.createElement("video_rate");
                video_rate.appendChild(doc.createTextNode(video.getVideoRate() > 0
                        ? String.valueOf(video.getVideoRate()) : ""));
                videoElement.appendChild(video_rate);

                Element video_tag = doc.createElement("video_tag");
                video_tag.appendChild(doc.createTextNode(video.getVideoTag() != null
                        ? video.getVideoTag() : ""));
                videoElement.appendChild(video_tag);

            }

            resultString = createStringResponseFromDocument(doc);

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        }

        return resultString;
    }

    public static String prepareXMLResponse(boolean isSuccess) {

        String resultString = null;
        try {

            DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();

            // root elements
            Document doc = docBuilder.newDocument();
            Element rootElement = doc.createElement("operation");
            doc.appendChild(rootElement);

            Element result = doc.createElement("result");
            result.appendChild(doc.createTextNode(isSuccess ? "1" : "0"));
            rootElement.appendChild(result);

            resultString = createStringResponseFromDocument(doc);

        } catch (ParserConfigurationException pce) {
            pce.printStackTrace();
        }

        return resultString;
    }

    private static String createStringResponseFromDocument(Document doc) {
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer;
        try {
            transformer = tf.newTransformer();

            transformer.transform(domSource, result);
        } catch (TransformerException e) {
            e.printStackTrace();
        }

        return writer.toString();
    }
}
